package com.java1234.controller;

import com.java1234.entity.Group;
import com.java1234.entity.PageBean;
import com.java1234.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.zip.ZipInputStream;

/**
 * Created by dengwubo on 2017/8/24.
 */
@Controller
@RequestMapping("/deploy")
public class DeployController {
    @Autowired
    private RepositoryService repositoryService;

    /**
     * 流程部署查询
     * @param page
     * @param rows
     * @param s_name
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "page",required = false)String page, @RequestParam(value = "rows",required = false)String rows,
                      @RequestParam(value = "s_name",required = false,defaultValue = "") String s_name, HttpServletResponse response)throws Exception{
        PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));

        List<Deployment> deployments =  repositoryService.createDeploymentQuery() // 创建流程部署查询
        .orderByDeploymenTime().desc() // 根据部署时间降序排列
        .deploymentNameLike("%"+s_name+"%")
        .listPage(pageBean.getStart(),pageBean.getPageSize()); // 返回带分页的结果集合

        long total = repositoryService.createDeploymentQuery().deploymentNameLike("%"+s_name+"%").count();// 获取总记录数


        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"resources"});
        jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd hh:mm:ss"));

        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deployments,jsonConfig);
        result.put("rows",jsonArray);
        result.put("total",total);
        ResponseUtil.write(response,result);


        return null;
    }

    /**
     * 上传流程部署文件进行部署
     * @param deployFile
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/deploy")
    public String deploy(@RequestParam("deployFile")MultipartFile deployFile,HttpServletResponse response)throws Exception{
        repositoryService.createDeployment() // 创建部署
        .name(deployFile.getOriginalFilename()) //流程名称
        .addZipInputStream(new ZipInputStream(deployFile.getInputStream()))
                .deploy(); //部署
        JSONObject result = new JSONObject();
        result.put("success",true);
        ResponseUtil.write(response,result);
        return null;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "ids",required = false)String ids,HttpServletResponse response)throws Exception{
        String[] idsStr = ids.split(",");
        Arrays.asList(idsStr).forEach(id->{
            repositoryService.deleteDeployment(id,true);
        });
        JSONObject result = new JSONObject();
        result.put("success",true);
        ResponseUtil.write(response,result);

        return null;
    }


}
